<template>
  <el-dialog :title="title + '部门'" :visible="showDialog" @close="close">
    <!-- 绘制表单 -->
    <el-form ref="form" :model="formDate" :rules="formRules" label-width="140px" :inline="false" size="normal">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formDate.name" placeholder="请输入部门名称" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formDate.code" placeholder="请输入部门编码" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="formDate.managerId" placeholder="请选择">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formDate.introduce" type="textarea" placeholder="请输入部门介绍" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="btnOk">确定</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>

  </el-dialog>
</template>
<script>
import { chooseDepartmentApi, getDepartmentApi, addDepartmentApi, infoDepartmentApi, editDepartmentApi } from '@/api/department'
export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    parentId: {
      type: [Number, String],
      default: ''
    }
  },

  data() {
    return {
      formDate: {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      },
      formRules: {
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 2, max: 10, message: '2-10个字符', trigger: 'blur' },
          { trigger: 'blur', validator: async(rules, value, cb) => {
            const res = await getDepartmentApi()
            // 是否已经存在
            if (this.formDate.id) {
              // 修改
              res.filter(item => item.id !== this.formDate.id && item.code === value).length ? cb(new Error('部门编码已经存在')) : cb()
            } else {
              // 添加
              res.filter(item => item.code === value).length ? cb(new Error('部门编码已经存在')) : cb()
            }
          } }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 2, max: 100, message: '2-100个字符', trigger: 'blur' }
        ],
        managerId: [{ required: true, message: '请输入部门负责人名字', trigger: 'blur' }],
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '2-10个字符', trigger: 'blur' },
          { trigger: 'blur', validator: async(rules, value, cb) => {
            const res = await getDepartmentApi()
            if (this.formDate.id) {
              // 修改
              res.filter(item => item.id !== this.formDate.id && item.name === value).length ? cb(new Error('部门名称已经存在')) : cb()
            } else {
              // 添加
              res.filter(item => item.name === value).length ? cb(new Error('部门名称已经存在')) : cb()
            }
          } }
        ]
      },
      managerList: []
    }
  },
  computed: {
    title() {
      return this.formDate.id ? '修改' : '添加'
    }
  },
  created() {
    // 调用
    this.getManagerList()
  },
  methods: {
    close() {
      // 重置清空表单内容
      this.formDate = {
        code: '',
        introduce: '',
        managerId: '',
        name: ''
      }
      this.$refs.form.resetFields() // 重置表单
      this.$refs.form.clearValidate() // 移除校验
      this.$emit('update:showDialog', false)
    },
    // 获取部门负责人数据
    async getManagerList() {
      // 发送请求
      const res = await chooseDepartmentApi()
      // 赋值变量
      this.managerList = res
    },
    // 提交事件
    async btnOk() {
      // 表单校验
      await this.$refs.form.validate()
      if (this.formDate.id) {
        // 发送修改请求
        await editDepartmentApi(this.formDate)
      } else {
        // 数据处理
        const formDate = { ...this.formDate, pid: this.parentId }
        // 发送添加请求
        await addDepartmentApi(formDate)
      }
      // 提示成功
      this.$message.success('添加成功')
      // 关闭
      this.close()
      // 通知列表更新
      this.$emit('updateList')
    },
    async reviewFormDate(id) {
      // 发送请求
      const res = await infoDepartmentApi(id)
      // 赋值变量
      this.formDate = res
    }
  }
}
</script>
